public class work3_better {
    public static void printPrimesOptimized() {
        int max = 20000;
        boolean[] isPrime = new boolean[max + 1];
        for (int i = 2; i <= max; i++) isPrime[i] = true;

        for (int i = 2; i * i <= max; i++) {
            if (isPrime[i]) {
                for (int j = i * i; j <= max; j += i) {
                    isPrime[j] = false;
                }
            }
        }

        int count = 0;
        for (int i = 2; i <= max; i++) {
            if (isPrime[i]) {
                System.out.print(i + "\t");
                if (++count % 5 == 0) System.out.println();
            }
        }
    }

    public static void main(String[] args) {
        printPrimesOptimized();
    }
}